#pragma once

#include <vector>
#include <memory>
#include <string>
#include <fstream>
#include <iostream>

#include "CSerialPort/SerialPort.h"
#include "CSerialPort/SerialPortInfo.h"
#include <gps.h>

class GPSListener : public itas109::CSerialPortListener, public itas109::CSerialPortHotPlugListener
{

public:
    GPSListener(itas109::CSerialPort *sp)
        : p_sp(sp)
    {
        // connect for read
        sp->connectReadEvent(this);
        // connect for hot plug
        sp->connectHotPlugEvent(this);
    };

    void openlog(std::string filename)
    {
        if (ofs.is_open())
        {
            ofs.close();
        }

        ofs.open(filename, std::ios::out | std::ios::binary);
    }

    double lon(void) const
    {
        return m_gga.lon;
    }

    char lon_dir(void) const
    {
        return m_gga.lon_dir;
    }
    double lat(void) const
    {
        return m_gga.lat;
    }

    char lat_dir(void) const
    {
        return m_gga.lat_dir;
    }

    double alt(void) const
    {
        return m_gga.alt;
    }
    double hdop(void) const
    {
        return m_gga.hdop;
    }

protected:
    bool gpsNewFrameNMEA(char c);

    void onReadEvent(const char *portName, unsigned int readBufferLen);

    void onHotPlugEvent(const char *portName, int isAdd);

public:
    // 可用串口列表
    std::vector<itas109::SerialPortInfo> m_availablePortsList;

private:
    // 串口句柄
    itas109::CSerialPort *p_sp;
    GGA m_gga;
    std::fstream ofs;
};